package pv3.queue;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Random;

/**
 *
 * @author Administrator
 *
 */
public class JMSProducer {

	private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;
	private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;
	private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;
	private static final int SENDNUM=10000;
	
	public static void main(String[] args) {
		
		ConnectionFactory connectionFactory;
		Connection connection = null;
		Session session;
		Destination destination;
		MessageProducer messageProducer;
		

//		connectionFactory=new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
		connectionFactory = new ActiveMQConnectionFactory("lili","lili",ActiveMQConnection.DEFAULT_BROKER_URL);
		try {
			connection=connectionFactory.createConnection();
			connection.start();
			session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
			destination=session.createQueue("LogQueue");
			messageProducer=session.createProducer(destination);
			sendMessage(session, messageProducer);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(connection!=null){
				try {
					connection.close();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * @param session
	 * @param messageProducer
	 * @throws Exception
	 */
	public static void sendMessage(Session session,MessageProducer messageProducer)throws Exception{
		
		
		
		for(int i=0;i<JMSProducer.SENDNUM;i++){
			String msg = "192.168.111."+i+" "+System.currentTimeMillis()+" "+"requestMethod"+i+ " "+ "www.baidu.com/"+""+new Random().nextInt(100)+".html";
			TextMessage message=session.createTextMessage(msg);
			System.out.println("生产者发送消息为：{ "+msg+" }");
			messageProducer.send(message);
		}
		
		
	}
}
